package com.example.finalexam;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private MyOpenHelper myOpenHelper;
    private List<Person> list;
    private ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyOpenHelper myOpenHelper = new MyOpenHelper(this);
        //创建数据库 打开或者创建数据库 如果是第一次调用则是创建 如果磁盘满了则返回一个只读的数据库
        myOpenHelper.getReadableDatabase();
        //创建Person集合
        list = new ArrayList<Person>();
        //找到控件
        lv = (ListView) findViewById(R.id.lv);
    }
    //增加两条条记录
    public void click1(View v)
    {
        //获取数据库对象
        SQLiteDatabase db = myOpenHelper.getWritableDatabase();
        //执行一条SQL语句
        ContentValues values = new ContentValues();
        values.put("id", "1000");
        values.put("type", "员工");
        values.put("id", 1001);
        values.put("type", "经理");
        long insert = db.insert("cdinfo",null, values);
        //数据库使用完成后要关闭数据库
        db.close();
        if (insert > 0)
        {
            Toast.makeText(getApplicationContext(), "插入成功", Toast.LENGTH_LONG);
        }
        else {
            Toast.makeText(getApplicationContext(), "插入失败", Toast.LENGTH_LONG);
        }
    }

    //查询
    public void click4(View v) {
        SQLiteDatabase db = myOpenHelper.getWritableDatabase();
        //用谷歌封装好的类实现
        //清理lists
        list.clear();
        Cursor cursor = db.query("cdinfo", null, "type=?", new String[]{"员工"}, null, null, null);
        if (cursor!=null&&cursor.getCount()>0){
            while(cursor.moveToNext()){
                //columnIndex打表列的索引
                //封装javabean
                Person person=new Person();
                person.setId(cursor.getInt(1));
                person.setType(cursor.getString(2));
                //把javabean对象加入到集合中
                list.add(person);
            }
            lv.setAdapter(new MyAdepter());
        }
        db.close();
    }


    public  class MyAdepter extends BaseAdapter
    {

        @Override
        public int getCount() {
            //设置显示条目
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           View v;
           if (convertView == null)
           {
               //创建新的view对象
               v = View.inflate(getApplicationContext(), R.layout.item, null);
           }
           else {
               v = convertView;
           }
           //找到控件
            TextView tv_id = (TextView) v.findViewById(R.id.id);
            TextView tv_type = (TextView) v.findViewById(R.id.type);
            /*
              用来显示数据
             *去集合里面去数据，用get方法，
             * 位置使用position，找到当前显示的位置
             */
            Person person=list.get(position);
            tv_id.setText(person.getId());
            tv_type.setText(person.getType());
            return v;
        }
    }
}
